import { useRequest } from "ahooks";
import {
  getBlogListMapper,
  createBlogMapper,
  updateBlogByIdMapper,
  getBlogByIDMapper,
} from "../sql";
import { GetBlogListDTO } from "../types";
import { toast } from "sonner";

export const getBlogList = (params: GetBlogListDTO) => {
  return useRequest(() => getBlogListMapper(params), {
    refreshDeps: [params],
  });
};

export const getBlogById = (id: string) => {
  return useRequest(() => getBlogByIDMapper(id), {
    refreshDeps: [id],
  });
};

export const createBlog = () => {
  return useRequest(createBlogMapper, {
    manual: true,
    onSuccess: () => {
      toast.success("创建成功");
    },
    onError(e) {
      toast.error(e.message);
    },
  });
};

export const updateBlog = () => {
  return useRequest(updateBlogByIdMapper, {
    manual: true,
    onSuccess: () => {
      toast.success("更新成功");
    },
    onError(e) {
      toast.error(e.message);
    },
  });
};
